//
//  XingChengViewController.m
//  Trip
//
//  Created by dlios on 15-7-14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "XingChengViewController.h"
#import "XingChengInfTableViewCell.h"
#import "XingChengInfViewController.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "UIViewController+Example.h"
#import "MJRefresh.h"

static const CGFloat MJDuration = 3.0;
@interface XingChengViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *myTableView;

@end

@implementation XingChengViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self headlerData];
    [self exampleUpPush];
}

- (void)exampleUpPush
{
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadNewData];
        
    }];
}

- (void)loadNewData
{
    [self headlerData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.myTableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.myTableView.header endRefreshing];
    });
}

- (void)headlerData
{
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/plans/%@.json?page=1", self.str];
    [AFNetworkHandler GetWithAFNByURL:url completion:^(id result) {
       
        self.souArr = result;
        self.navigationItem.title = [NSString stringWithFormat:@"%@行程", self.title];
        [self setTabView];
        [self.myTableView reloadData];
    }];
}

- (void)setTabView
{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.view addSubview:_myTableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 330;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XingChengInfViewController *xcInfVC = [[XingChengInfViewController alloc] init];
    xcInfVC.str = [[self.souArr objectAtIndex:indexPath.row] objectForKey:@"id"];
    [self.navigationController pushViewController:xcInfVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.souArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    XingChengInfTableViewCell *xcCell = [[XingChengInfTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
//    [xcCell.images setImageWithURL:[NSURL URLWithString:[[self.souArr objectAtIndex:indexPath.row] objectForKey:@"image_url"]]];
    [xcCell.images setImageWithURL:[NSURL URLWithString:[[self.souArr objectAtIndex:indexPath.row] objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
    xcCell.label1.text = [[self.souArr objectAtIndex:indexPath.row] objectForKey:@"name"];
    xcCell.label2.text = [NSString stringWithFormat:@"%@日", [[self.souArr objectAtIndex:indexPath.row] objectForKey:@"plan_days_count"]];
    xcCell.label3.text = [NSString stringWithFormat:@"%@个旅行地", [[self.souArr objectAtIndex:indexPath.row] objectForKey:@"plan_nodes_count"]];
    xcCell.label4.numberOfLines = 0;
    xcCell.label4.text = [[self.souArr objectAtIndex:indexPath.row] objectForKey:@"description"];
    return xcCell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
